package cn.cs.service.impl;

import cn.cs.client.RestRepairEvaluateClient;
import cn.cs.client.RestRepairOrderClient;
import cn.cs.client.RestRepairUserClient;
import cn.cs.common.*;
import cn.cs.pojo.RepOrder;
import cn.cs.pojo.RepairEvaluate;
import cn.cs.pojo.RepairUser;
import cn.cs.service.RepairEvaluateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class RepairEvaluateServiceImpl implements RepairEvaluateService {

    @Resource
    private RestRepairOrderClient repairOrderClient;

    @Resource
    private RestRepairEvaluateClient repairEvaluateClient;

    @Resource
    private RestRepairUserClient repairUserClient;



    public List<Map<String, Object>> getRepairEvaluateListByMap(Map<String,Object> param)throws Exception{
        List<Map<String, Object>> list = repairEvaluateClient.getRepairEvaluateListByMap(param);
        for(Map<String, Object> a:list){
            String repUserId = a.get("repUserId").toString();
            RepairUser repairUser = repairUserClient.getRepairUserById(repUserId);
            a.put("nickname", repairUser.getPersonName());a.put("image_url", repairUser.getImage_url());
        }
        return repairEvaluateClient.getRepairEvaluateListByMap(param);
    }

    public Integer addRepairEvaluate(RepairEvaluate repairEvaluate)throws Exception{
            return repairEvaluateClient.addRepairEvaluate(repairEvaluate);
    }

    @Override
    public Dto registerRepairEvaluate(HttpServletRequest request, String id, String clientUserId, String repUserId, String orderId, String score, String content, String createTime) throws Exception {
        RepairEvaluate repairEvaluate=new RepairEvaluate();
        repairEvaluate.setId(id);
        repairEvaluate.setClientUserId(clientUserId);
        repairEvaluate.setRepUserId(repUserId);
        repairEvaluate.setOrderId(orderId);
        repairEvaluate.setScore(score);
        repairEvaluate.setContent(content);
        repairEvaluate.setCreateTime(createTime);
        repairEvaluateClient.addRepairEvaluate(repairEvaluate);


        //改变订单状态
        RepOrder repOrder = new RepOrder();
        repOrder.setId(orderId);
        repOrder.setOrderState("4");
        repairOrderClient.updateRapairOrderStatus(repOrder);

        //根据订单查找预约单Id，修改预约单状态
        repairOrderClient.updateAppOrderStatus(orderId,"5");
        //修改维修师傅星级
        repairOrderClient.updateRepairUserStar(Long.valueOf(score), orderId);

        return DtoUtil.returnSuccess("评价成功", repairEvaluateClient.getAppOrderByOrderId(orderId));
    }

}
